<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="公司名" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户经理" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入所属客户经理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="协同人-客户经理" prop="collaborator">
        <el-input
          v-model="queryParams.collaborator"
          placeholder="请输入协同人-客户经理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="联系人姓名" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="联系人身份" prop="contactIdentity">
        <el-input
          v-model="queryParams.contactIdentity"
          placeholder="请输入联系人身份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="客户来源" prop="customerSource">
        <el-select v-model="queryParams.customerSource" placeholder="请选择客户来源" clearable>
          <el-option
            v-for="dict in dict.type.customer_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
<!--        <el-input
          v-model="queryParams.customerSource"
          placeholder="请输入客户来源"
          clearable
          @keyup.enter.native="handleQuery"
        />-->
      </el-form-item>
<!--      <el-form-item label="客户行业" prop="industry">
        <el-select v-model="queryParams.industry" placeholder="请选择客户行业" clearable>
          <el-option
            v-for="dict in dict.type.customer_industry"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="所属区域" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入所属区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="社会统一信用编码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入社会统一信用编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankId">
        <el-input
          v-model="queryParams.bankId"
          placeholder="请输入银行账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行" prop="bankOpening">
        <el-input
          v-model="queryParams.bankOpening"
          placeholder="请输入开户行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dianlan:customer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dianlan:customer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dianlan:customer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dianlan:customer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" />-->
      <el-table-column label="公司名" align="center" prop="companyName" />
      <el-table-column label="所属客户经理" align="center" prop="userName" />
<!--      <el-table-column label="协同人-客户经理" align="center" prop="collaborator" />-->
      <el-table-column label="联系人姓名" align="center" prop="contactPerson" />
      <el-table-column label="联系人电话" align="center" prop="contactPhone" />
      <el-table-column label="联系人身份" align="center" prop="contactIdentity" />
      <el-table-column label="客户来源" align="center" prop="customerSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_source" :value="scope.row.customerSource"/>
        </template>
      </el-table-column>
      <el-table-column label="客户状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="客户行业" align="center" prop="industry">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_industry" :value="scope.row.industry"/>
        </template>
      </el-table-column>
      <el-table-column label="所属区域" align="center" prop="region" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="社会统一信用编码" align="center" prop="creditCode" />
      <el-table-column label="银行账号" align="center" prop="bankId" />
      <el-table-column label="开户行" align="center" prop="bankOpening" />
      <el-table-column label="图片文件" align="center" prop="file" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.file" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dianlan:customer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dianlan:customer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公司名" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名" />
        </el-form-item>
<!--        <el-form-item label="所属客户经理" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入所属客户经理" />
        </el-form-item>-->
        <el-form-item label="协同人" prop="collaborator">
          <el-input v-model="form.collaborator" placeholder="请输入协同人"/>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="联系人身份" prop="contactIdentity">
          <el-input v-model="form.contactIdentity" placeholder="请输入联系人身份" />
        </el-form-item>
        <el-form-item label="客户来源" prop="customerSource">
          <el-select v-model="form.customerSource" placeholder="请选择客户来源">
            <el-option
              v-for="dict in dict.type.customer_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择客户状态">
            <el-option
              v-for="dict in dict.type.customer_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户行业" prop="industry">
          <el-select v-model="form.industry" placeholder="请选择客户行业">
            <el-option
              v-for="dict in dict.type.customer_industry"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域" prop="region">
          <el-input v-model="form.region" placeholder="请输入所属区域" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="社会统一信用编码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入社会统一信用编码" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankId">
          <el-input v-model="form.bankId" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="开户行" prop="bankOpening">
          <el-input v-model="form.bankOpening" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="图片文件" prop="file">
          <image-upload v-model="form.file"/>
        </el-form-item>
        <el-form-item label="星级" prop="level">
          <el-rate
            v-model="form.level"
            :texts="['无效', '边缘', '日常', '重要', '核心']"
            show-text
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/dianlan/customer/customer";

export default {
  name: "Customer",
  dicts: ['customer_industry','customer_source','customer_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        userName: null,
        collaborator: null,
        contactPerson: null,
        contactPhone: null,
        contactIdentity: null,
        customerSource: null,
        status: null,
        industry: null,
        region: null,
        address: null,
        creditCode: null,
        bankId: null,
        bankOpening: null,
        file: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          {required: true, message: "不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        companyName: null,
        userName: null,
        collaborator: null,
        contactPerson: null,
        contactPhone: null,
        contactIdentity: null,
        customerSource: null,
        status: null,
        industry: null,
        region: null,
        address: null,
        remark: null,
        creditCode: null,
        bankId: null,
        bankOpening: null,
        file: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户管理编号为"' + ids + '"的数据项？').then(function() {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dianlan/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
